package com.equdao.car_violation_inquiry.controller;

import com.equdao.car_violation_inquiry.pojo.Result;
import com.equdao.car_violation_inquiry.pojo.WebPageLoginDO;
import com.equdao.car_violation_inquiry.service.ViolationLoginService;
import com.equdao.car_violation_inquiry.utils.CookieUtil;
import com.equdao.car_violation_inquiry.utils.MD5Utils;
import com.equdao.car_violation_inquiry.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @Author: 杨忠强
 * @Date: 2018/8/1 10:42
 * @Description:
 */
@Controller
@Slf4j

public class WebPageLoginController {

    @Autowired
    ViolationLoginService violationLoginService;

    @PostMapping("/doLogin")
    public String doLogin(String userName, String password, String role, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        log.info("用户"+userName+"尝试登录");
        System.out.println("用户输入的密码"+password);
        String paswordMd5 = MD5Utils.Md5(password);
        WebPageLoginDO webPageLoginDO = violationLoginService.doLogin(userName, paswordMd5);
        if (webPageLoginDO != null) {
            //登录成功
            log.info("用户"+userName+"登陆成功");
            String token = UUID.randomUUID().toString();
            CookieUtil.set(response,"token",token,7200);
            return "common/index.html";
        } else {
            System.out.println("错误");
            return "common/error.html";
        }


    }


}
